﻿namespace Eneta.Portal.Events
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using Microsoft.SharePoint;

    public class AllEventsControl : UserControl
    {
        protected Repeater rpt;

        private SPWeb web;
        private SPList list;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            web = SPContext.Current.Web;
            SPListCollection lists = web.Lists;

            list = web.Lists["Lehed"];
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            SPQuery q = new SPQuery
            {
                Query = "<Where><And><Eq><FieldRef Name='ContentType' /><Value Type='Text'>Event</Value></Eq>" +
                    "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Kinnitatud</Value></Eq></And></Where>"
            };

            SPListItemCollection items = list.GetItems(q);

            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("EventTitle");
            dt.Columns.Add("Place");
            dt.Columns.Add("ShortDescription");
            dt.Columns.Add("Day");
            dt.Columns.Add("Month");
            dt.Columns.Add("DatesRange");
            dt.Columns.Add("TimeRange");
            dt.Columns.Add("Url");

            const string DATEFORMAT = "dd.MM.yyyy";

            foreach (SPListItem curItem in items)
            {
                DataRow dr = dt.NewRow();

                DateTime? eventDate = null;
                DateTime? eventEndDate = null;
                if (curItem["EventStartDate"] != null)
                {
                    eventDate = (DateTime)curItem["EventStartDate"];
                }

                if (curItem["EventEndDate"] != null)
                {
                    eventEndDate = (DateTime)curItem["EventEndDate"];
                }

                dr["ID"] = curItem.ID;
                dr["Url"] = web.Url + "/" + curItem.Url;
                dr["EventTitle"] = curItem.Title;
                dr["Place"] = curItem["Place"] as string ?? string.Empty;
                dr["ShortDescription"] = curItem["ShortDescription"] as string ?? string.Empty;
                if (eventDate.HasValue)
                {
                    dr["Day"] = eventDate.Value.Day.ToString();
                    dr["Month"] = eventDate.Value.ToString("MMMM");
                    if (eventEndDate.HasValue && eventDate.Value.Date != eventEndDate.Value.Date)
                    {
                        dr["DatesRange"] = eventDate.Value.ToString(DATEFORMAT) + " kuni " + eventEndDate.Value.ToString(DATEFORMAT);
                    }
                    else
                    {
                        dr["DatesRange"] = eventDate.Value.ToString(DATEFORMAT);
                    }

                    if (eventEndDate.HasValue && eventDate.Value.TimeOfDay != eventEndDate.Value.TimeOfDay)
                    {
                        dr["TimeRange"] = eventDate.Value.ToString("H.mm") + " - " + eventEndDate.Value.ToString("H.mm");
                    }
                    else
                    {
                        dr["c"] = eventDate.Value.ToString("H.mm");
                    }
                }
                else
                {
                    dr["Day"] = string.Empty;
                    dr["Month"] = string.Empty;
                }

                dt.Rows.Add(dr);
            }

            rpt.DataSource = dt;
            rpt.DataBind();
        }
    }
}
